<template>
	<view class="inwardPlanToCarrier">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :statusBar="true" title="授权承运商" fixed="true" left-icon="left" left-text="返回" @clickLeft="back"></uni-nav-bar>
		<view class="inwardPlanToCarrier_top">
			<!-- 搜索 -->
			<!-- 搜索 -->
			<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0"></dil-search>
		</view>
		<view class="inwardPlanToCarrier_main">
			<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
		</view>
		<view class="inwardPlanToCarrier_floot"></view>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		return {
			options: {
				requestUrl: '/api/v1/ams/getProductPlanList?apiId=626',
				requestQuery: {},
				cardSlotList: [
					{
						label: '拒绝',
						icon: 'wlIcon-refuse',
						emitEvent: 'refuse',
						showCondition: "['已审批'].includes(item.status)"
					},
					{
						label: '授权承运商',
						icon: 'wlIcon-xinzeng',
						emitEvent: 'dispatchToCarrier'
					},
					{
						label: '停用',
						icon: 'wlIcon-tingyong',
						emitEvent: 'deactivated',
						arguments: {
							status: 2
						},
						showCondition: "item.planStatus != null && ['已下发','已派车'].includes(item.planStatus)"
					},
					{
						label: '启用',
						icon: 'wlIcon-qiyong',
						emitEvent: 'deactivated',
						arguments: {
							status: 1
						},
						showCondition: "['停用'].includes(item.planStatus)"
					},
					{
						label: '派车',
						icon: 'wlIcon-paicheguanli',
						emitEvent: 'transOrderAdd',
					}
				],
				columnDataList: [],
				totalNum: 0,
				rowDataList: []
			}
		};
	},
	onShow() {
		uni.$emit('onShow', 1);
		console.log("show-inwardPlanToCarrier");
		this.getOrderSection();
	},
	mounted() {
		this.getOrderSectionDebounce = this.Tool.debounce((data) => {
			this.getOrderSection(data);
		}, 1500);
	},
	methods: {
		back() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		onSearch(item) {
			this.getOrderSectionDebounce(item);
		},
		async getOrderSection(data = {}) {
			this.options.requestQuery = { ...data };
		},
		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},
		transOrderBrowse(item) {
			console.log(item);
			console.log('运输订单详情');
		},
		refuse(item) {
			uni.showModal({
				cancelText: '取消',
				confirmText: '确定',
				title: '提示',
				content: '是否拒绝该需求(仅能拒绝未制订计划的需求)',
				success: (res) => {
					if (res.confirm) {
						this.refuseInwardAppointItem(item);
					} else {
						uni.showToast({
							title: '用户取消操作',
							type: 'info',
							duration: 2000
						});
					}
				}
			});
		},
		async refuseInwardAppointItem(item) {
			let data = {
				approve: 'refuseAddPlan',
				...item
			};
			const res = await this.$myRequest({
				url: '/api/v1/ams/productionRequirementChange',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					this.getOrderSection();
				}, 2200);
			} else {
				uni.showToast({
					title: res.data.message,
					icon: 'error',
					duration: 2000
				});
			}
		},
		deactivated(item) {
			let titleTip = item.status == 1 ? '是否启用该计划（已停用的运单无法恢复）' : '是否停用该计划（系统将自动关闭该计划未接收的运输订单）';
			uni.showModal({
				cancelText: '取消',
				confirmText: '确定',
				title: '提示',
				content: titleTip,
				success: (res) => {
					if (res.confirm) {
						this.deactivatedPlanItem(item);
					} else {
						uni.showToast({
							title: '用户取消操作',
							type: 'info',
							duration: 2000
						});
					}
				}
			});
		},
		async deactivatedPlanItem(item) {
			let data = {
				status: item.status,
				mapList: [{ transPlanId: item.transPlanId }]
			};
			const res = await this.$myRequest({
				url: '/api/v1/ams/deactivatedPlan',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'error'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					this.getOrderSection();
				}, 2200);
			} else {
				uni.showToast({
					title: res.data.message,
					icon: 'error',
					duration: 2000
				});
			}
		},
		dispatchToCarrier(item) {
			this.updateInwardAppointItem(item);
			uni.navigateTo({
				url: '/pages/transPlan/inwardPlanAdd'
			});
		},
		transOrderAdd(item) {
			this.updateInwardTransOrderItem(item);
			if(item.planSerialNumber){
				uni.navigateTo({
					url: '/pages/transOrder/inwardTransOrderAdd'
				});
			}else{
				uni.navigateTo({
					url: '/pages/transOrder/inwardTransOrderAdd_NoPlan'
				});
			}
		},
		//vuex
		...mapMutations('inward', ['updateInwardAppointItem','updateInwardTransOrderItem'])
	}
};
</script>

<style lang="scss" scoped>
.inwardPlanToCarrier {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	&_top {
		background-color: #fff;
	}
	&_main {
	}
	&_floot {
		background-color: #fff;
	}
}
</style>
